#include "rclcpp/rclcpp.hpp"
#include "route_interfaces/srv/route.hpp"

class route_client : public rclcpp::Node
{
private:
    /* data */
    rclcpp::Client<route_interfaces::srv::Route>::SharedPtr client;
public:
    route_client(std::string srv_name);
    void request_route()
    {
        while (!client->wait_for_service(std::chrono::seconds(1)))
        {
            //等待时检测rclcpp的状态
            if (!rclcpp::ok())
            {
                RCLCPP_ERROR(this->get_logger(), "等待服务的过程中被打断...");
                return;
            }
            RCLCPP_INFO(this->get_logger(), "等待服务端上线中");
        }
        auto request = std::make_shared<route_interfaces::srv::Route_Request>();
        request->len = 3;
        request->wth = 3;
        request->map = {1,2,3,3,3,3,4,4,4};
        client->async_send_request(request,std::bind(&route_client::route_call_back,this,std::placeholders::_1));
    }
    void route_call_back(rclcpp::Client<route_interfaces::srv::Route>::SharedFuture response)
    {
        //路由处理
        auto result = response.get();
        for(int i = 0;i < 9;i ++)
        {
            RCLCPP_INFO(this->get_logger(),"get map:%d",result->step[i]);
        }
    }
};

route_client::route_client(std::string srv_name):Node(srv_name)
{
    // RCLCPP_INFO(this->get_logger(),"route server start.");
    // RCLCPP_INFO(this->get_logger(),"writing for request.");
    client = this->create_client<route_interfaces::srv::Route>("route_server");    
}

int main(int argc, char **argv)
{
    rclcpp::init(argc,argv);
    auto node = std::make_shared<route_client>("route_server");
    node->request_route();
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}

